﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Divan;
using libStorage;

namespace libCouchStorage
{
    public class CouchDBProvider : IStorageProvider
    {
        private CouchServer _Server;
        private ICouchDatabase _DataBase;
        private CouchDBConfig _Config;
        private Dictionary<object, object> _ContainerLokkupTable = new Dictionary<object, object>();
        private CouchDBConfigControl _configControl; 

        public bool init()
        {
            bool bRet = true;

            try
            {
                string configFile = Application.UserAppDataPath + "CouchConfig.xml";
                _Config = CouchDBConfig.load(configFile);
                _Server = new CouchServer(_Config.Server, _Config.Port, _Config.User, _Config.Pass);

                if (!_Server.HasDatabase(_Config.Database)) _Server.CreateDatabase(_Config.Database);

                _DataBase = _Server.GetDatabase(_Config.Database);
            }
            catch (Exception)
            {
                bRet = false;
            }
          

            return bRet;
        }

        public void Store<T>(List<T> ToStore)
        {

            foreach (T obj in ToStore)
            {

                CouchDBDataContainer<T> container; 

                if (_ContainerLokkupTable.ContainsKey(obj))
                {
                    container = (CouchDBDataContainer<T>)_ContainerLokkupTable[obj];
                }
                else
                {
                    container = new CouchDBDataContainer<T>(obj);
                    _ContainerLokkupTable.Add(obj, container);
                }

                _DataBase.SaveDocument(container);
                 
            }
                       
        }

        public List<T> Load<T>()
        {
            IEnumerable<CouchDBDataContainer<T>> allDocs = _DataBase.GetAllDocuments<CouchDBDataContainer<T>>();
            List<T> ret = new List<T>();

            foreach (CouchDBDataContainer<T> container in allDocs)
            {
                ret.Add(container.getData());

                if (_ContainerLokkupTable.ContainsKey(container.getData()))
                {
                    _ContainerLokkupTable.Remove(container.getData());
                }

                _ContainerLokkupTable.Add(container.getData(), container);

            }

            return ret;
        }

        public List<T> Load<T>(T Pattern)
        {
            throw new NotImplementedException();
        }

        public UserControl getConfigControl()
        {
            if (_configControl == null) _configControl = new CouchDBConfigControl();

            return _configControl;
        }

        public string Name()
        {
            return "CouchDB";
        }

        public string Discription()
        {
            return "This is a storageengine that stores data in a CouchDB server";
        }

        
    }
}
